/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanControlOfMatchingPanel.java
 *
 * Created on 17-jul-2011, 19:08:31
 */

package br.usp.ime.faguilar.guis.matching;

import DatabaseMathExpressions.DatabaseTestFunctions;
import br.usp.ime.faguilar.guis.PanLabeling;
import Matching.Graphics.GMatching;
import Matching.IncorrectMatching;
import Matching.MatchingEvaluator;
import Matching.MatchingResult;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author frank
 */
public class PanControlOfMatchingPanel extends JPanel {


    private int[][] initialMatching;

    private DatabaseTestFunctions databaseTest;

    /** Creates new form PanControlOfMatchingPanel */
    public PanControlOfMatchingPanel() {
        initComponents();
        initialMatching=null;
        databaseTest=new DatabaseTestFunctions();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        panControl = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        spinMatchLines = new javax.swing.JSpinner();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        butEvaluated = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        panMatching = new br.usp.ime.faguilar.guis.matching.PanMatching();

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 76, Short.MAX_VALUE)
        );

        setLayout(new java.awt.BorderLayout());

        panControl.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        panControl.setLayout(new java.awt.BorderLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("View"));

        spinMatchLines.setEnabled(false);
        spinMatchLines.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinMatchLinesStateChanged(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Complete matching");
        jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButton1MouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("By groups:");
        jRadioButton2.setEnabled(false);
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jRadioButton1)
                .add(68, 68, 68)
                .add(jRadioButton2)
                .add(18, 18, 18)
                .add(spinMatchLines, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 79, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jRadioButton1)
                        .add(jRadioButton2))
                    .add(spinMatchLines, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panControl.add(jPanel2, java.awt.BorderLayout.CENTER);

        butEvaluated.setText("Evaluated");
        butEvaluated.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butEvaluatedMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(butEvaluated)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(9, 9, 9)
                .add(butEvaluated, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 49, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        panControl.add(jPanel3, java.awt.BorderLayout.EAST);

        add(panControl, java.awt.BorderLayout.SOUTH);

        org.jdesktop.layout.GroupLayout panMatchingLayout = new org.jdesktop.layout.GroupLayout(panMatching);
        panMatching.setLayout(panMatchingLayout);
        panMatchingLayout.setHorizontalGroup(
            panMatchingLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 613, Short.MAX_VALUE)
        );
        panMatchingLayout.setVerticalGroup(
            panMatchingLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 313, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(panMatching);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void spinMatchLinesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinMatchLinesStateChanged
        // TODO add your handling code here:
        panMatching.setSelectedGroup(((Integer)this.spinMatchLines.getModel().getValue()-1));
        panMatching.repaint();
    }//GEN-LAST:event_spinMatchLinesStateChanged

    private void butEvaluatedMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butEvaluatedMouseClicked
        // TODO add your handling code here:
        if(initialMatching!=null){
            ArrayList<IncorrectMatching> incorrectMatchings=MatchingEvaluator.evaluate(
                    initialMatching,panMatching.getMatchingMatrix());
            System.out.println("number of errors: "+incorrectMatchings.size());

            MatchingResult matchingResult=new MatchingResult();
            String idWriterOfInstance=((PanLabeling)panMatching.getPanManagerOfUserExpression()).getSelectedUE().getIdUser();
            String idWriterOfModel="preDefinedModel";
            String idModel=String.valueOf(((PanLabeling)panMatching.getPanManagerOfUserExpression()).getSelectedME().getId());
            String idInstance=String.valueOf(((PanLabeling)panMatching.getPanManagerOfUserExpression()).getSelectedUE().getId());
            int numberOfSymbols=((PanLabeling)panMatching.getPanManagerOfUserExpression()).getSelectedME().getdMathExpression().size();
            int numberOfWrongLabels=incorrectMatchings.size();

            matchingResult.setIdInstance(idInstance);
            matchingResult.setIdModel(idModel);
            matchingResult.setWriterOfInstance(idWriterOfInstance);
            matchingResult.setWriterOfModel(idWriterOfModel);
            matchingResult.setNumberOfSymbols(numberOfSymbols);
            matchingResult.setWrongMatchings(incorrectMatchings);
            if(!databaseTest.isOpenConnection())
                databaseTest.openConnection();
            databaseTest.addMatchingResult(matchingResult);

        }
        ((PanLabeling)panMatching.getPanManagerOfUserExpression()).saveEvaluatedExpression();
    }//GEN-LAST:event_butEvaluatedMouseClicked

    private void jRadioButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton1MouseClicked
        // TODO add your handling code here:
        if(this.jRadioButton1.isSelected()){
            panMatching.getMatchingDrawer().setTypeOfDrawing(GMatching.DRAW_COMPLETE_MATCHING);
            this.spinMatchLines.setEnabled(false);
        }
        this.repaint();
    }//GEN-LAST:event_jRadioButton1MouseClicked

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        if(this.jRadioButton2.isSelected()){
            panMatching.getMatchingDrawer().setTypeOfDrawing(GMatching.DRAW_ONE_GROUP);
            panMatching.setSelectedGroup(((Integer)this.spinMatchLines.getModel().getValue()-1));
            //this.setLineColor(((Integer)this.spinMatchLines.getModel().getValue()-1));
            this.spinMatchLines.setEnabled(true);
        }
        this.repaint();
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    public void setEvaluatesToManagerOfUserExpression(boolean evaluated){
//        ((PanLabeling)this.panManagerOfUserExpression).setEvaluateddUserExpression(evaluated);
        this.butEvaluated.setEnabled(evaluated);
    }

    public void setEvaluatedExpressionInChBox(boolean evaluated){
//        this.chBoxUserExpressionCheked.setSelected(evaluested);
        this.butEvaluated.setEnabled(!evaluated);
    }
    
    public void setUpMatchingDrawer(){
        panMatching.setUpMatchingDrawer();
        
        initialMatching=(int[][])panMatching.getMatchingMatrix().clone();
//        for (int i = 0; i < initialMatching.length; i++) {
//            int[] is = initialMatching[i];
//            for (int j = 0; j < is.length; j++) {
//                int k = is[j];
//                System.out.print(k+" ");
//
//            }
//            System.out.println("");
//        }
        SpinnerModel model =
        new SpinnerNumberModel(1, //initial value
                               1, //min
                               panMatching.getNumerOfGroups(), //max
                               1); //step
        this.spinMatchLines.setModel(model);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butEvaluated;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panControl;
    private br.usp.ime.faguilar.guis.matching.PanMatching panMatching;
    private javax.swing.JSpinner spinMatchLines;
    // End of variables declaration//GEN-END:variables

   
    public void enableControlls(boolean enable){
//        this.chBShowCompleteMatch.setEnabled(enable);
//        this.chBoxUserExpressionCheked.setEnabled(enable);
        this.butEvaluated.setEnabled(enable);
        this.jRadioButton1.setEnabled(enable);
//        this.jRadioButton2.setEnabled(enable);
//        this.jButton1.setEnabled(enable);
//        this.spinMatchLines.setEnabled(enable);
    }

    public PanMatching getPanMatching() {
        return panMatching;
    }

    public void setPanMatching(PanMatching panMatching) {
        this.panMatching = panMatching;
    }


}
